package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcSingleContacts;
import com.cloud.bc.model.entity.BcSingleMates;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.entity.BcUserPhotos;
import com.cloud.bc.model.enums.PhotoEnum;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("m/center/single")
@AllArgsConstructor
public class MCenterSingleController extends BaseController {
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final BcSingleMatesService bcSingleMatesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcSingleContactsService bcSingleContactsService;

    /**
     * 编辑资料
     */
    @GetMapping("modify")
    public ModelAndView modify(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"编辑资料", "m/center/single/modify");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingles bcSingles = bcSinglesService.selectOne(
                new BcSingles().setUserId(userInfo.getId()));
        if (null != bcSingles) {
            view.addObject("singles", bcSingles);
        }
        if (StrUtil.isEmpty(bcSingles.getNickname()) ||
                Convert.toInt(bcSingles.getGender(), 0) == 0 ||
                null == bcSingles.getBirthday() ||
                StrUtil.isEmpty(bcSingles.getDistrict())
        ) {
            //如果没有昵称，性别，生日，所在地区 就跳转到第一步
            return new ModelAndView("redirect:/m/center/single/setupone");
        }
        if (StrUtil.isEmpty(bcSingles.getPhoto())) {
            //如果头象是空的，就跳转到第二步
            return new ModelAndView("redirect:/m/center/single/setuptwo");
        }
        view.addObject("singles_mate", bcSingleMatesService.selectOne(
                new BcSingleMates().setUserId(userInfo.getId())));
        BcUserPhotos bcUserPhotos = new BcUserPhotos();
        bcUserPhotos.setUserId(userInfo.getId());
        bcUserPhotos.setTitle(PhotoEnum.ALBUM.getTitle());
        List<BcUserPhotos> albumList = bcUserPhotosService.selectAll(bcUserPhotos);
        view.addObject("photo", albumList);
        return view;
    }

    @GetMapping("setupone")
    public ModelAndView setupone(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        ModelAndView view = baseView(request,"完善资料第一步", "m/center/single/setupone");
        BcSingles bcSingles = bcSinglesService.
                selectOne(new BcSingles().setUserId(userInfo.getId()));
        BcSingleContacts bcSingleContacts = bcSingleContactsService.selectOne(new BcSingleContacts().setUserId(userInfo.getId()));
        view.addObject("single", bcSingles);
        view.addObject("bcSingleContacts", bcSingleContacts);
        return view;
    }

    @GetMapping("setuptwo")
    public ModelAndView setuptwo(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        ModelAndView view = baseView(request,"完善资料第二步", "m/center/single/setuptwo");
        BcSingles bcSingles = bcSinglesService.
                selectOne(new BcSingles().setUserId(userInfo.getId()));
        if(!StrUtil.isEmpty(bcSingles.getPhoto())){
            //如果不是空的就跳转到个人中心吧
            return new ModelAndView("redirect:/m/center");
        }
        view.addObject("single", bcSingles);

        return view;
    }
}
